// 生成验证码图片并返回 { code, dataUrl }
export function generateCaptcha(width = 120, height = 40, length = 5) {
    const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789'
    let code = ''
    for (let i = 0; i < length; i++) {
        code += chars.charAt(Math.floor(Math.random() * chars.length))
    }

    const canvas = document.createElement('canvas')
    canvas.width = width
    canvas.height = height
    const ctx = canvas.getContext('2d')

    // 背景
    ctx.fillStyle = randomLightColor()
    ctx.fillRect(0, 0, width, height)

    // 干扰线
    for (let i = 0; i < 6; i++) {
        ctx.strokeStyle = randomDarkColor()
        ctx.beginPath()
        ctx.moveTo(Math.random() * width, Math.random() * height)
        ctx.lineTo(Math.random() * width, Math.random() * height)
        ctx.stroke()
    }

    // 文本绘制（每个字符旋转、位移）
    const fontSize = Math.floor(height * 0.7)
    ctx.font = `${fontSize}px sans-serif`
    ctx.textBaseline = 'middle'

    const charWidth = width / length
    for (let i = 0; i < code.length; i++) {
        const txt = code[i]
        const x = i * charWidth + charWidth / 2
        const y = height / 2 + (Math.random() - 0.5) * 6
        const angle = (Math.random() - 0.5) * 0.6 // -0.3 ~ 0.3 rad

        ctx.save()
        ctx.translate(x, y)
        ctx.rotate(angle)

        // 文字颜色
        ctx.fillStyle = randomDarkColor()
        ctx.fillText(txt, -fontSize / 2 + (Math.random() - 0.5) * 6, 0)
        ctx.restore()
    }

    // 干扰点
    for (let i = 0; i < 30; i++) {
        ctx.fillStyle = randomDarkColor()
        ctx.beginPath()
        ctx.arc(Math.random() * width, Math.random() * height, Math.random() * 1.5, 0, Math.PI * 2)
        ctx.fill()
    }

    // 轻微扭曲效果（像素级别的波浪） — 简单实现：将整张画布绘制到另一个画布的偏移位置
    const tmpCanvas = document.createElement('canvas')
    tmpCanvas.width = width
    tmpCanvas.height = height
    const tctx = tmpCanvas.getContext('2d')
    const amplitude = 3
    const period = 120
    const imageData = ctx.getImageData(0, 0, width, height)

    for (let y = 0; y < height; y++) {
        const offset = Math.floor(amplitude * Math.sin((2 * Math.PI * y) / period))
        if (offset === 0) {
            tctx.putImageData(ctx.getImageData(0, y, width, 1), 0, y)
        } else if (offset > 0) {
            tctx.putImageData(ctx.getImageData(0, y, width - offset, 1), offset, y)
        } else {
            tctx.putImageData(ctx.getImageData(-offset, y, width + offset, 1), 0, y)
        }
    }

    // 输出为 dataURL
    const dataUrl = tmpCanvas.toDataURL('image/png')
    return { code, dataUrl }
}

function randomLightColor() {
    const r = 200 + Math.floor(Math.random() * 55)
    const g = 200 + Math.floor(Math.random() * 55)
    const b = 200 + Math.floor(Math.random() * 55)
    return `rgb(${r},${g},${b})`
}

function randomDarkColor() {
    const r = 20 + Math.floor(Math.random() * 120)
    const g = 20 + Math.floor(Math.random() * 120)
    const b = 20 + Math.floor(Math.random() * 120)
    return `rgb(${r},${g},${b})`
}
